# 使用求解引擎一列一列铺画，每铺画一列即对movement的发生先后顺序进行固定

import PyStationCapacityData as pscdata
import PyStationCapacity as psc

AppliedSolverName = "Gurobi"
ObjFunc = "Mind"
Data = None
ObjVal = -1


def DoTrainByTrainIteration():
    global Data
    global AppliedSolverName
    global ObjFunc
    global ObjVal

    CandidateTrainList = list()
    ''' 尚未铺画的列车列表 '''

    psc.Data = Data  # 创建数据对象
    psc.AppliedSolverName = AppliedSolverName
    psc.ObjFunc = ObjFunc

    psc.Data.LoadData()  # 从文件中读取数据

    psc.Initialize()  # 初始化

    # 处理列车列表
    CandidateTrainList = psc.Data.TrainList
    psc.Data.TrainList = list()

    # 循环加车求解
    while CandidateTrainList.__len__() > 0:
        currentTrain = CandidateTrainList.pop()
        psc.Data.TrainList.append(currentTrain)

        print("\r\n--\r\n当前求解列车: {0}, 剩余列车数量: {1}.".format(
            currentTrain.ID, CandidateTrainList.__len__()))
        print("开始创建模型...")
        psc.BuildModel()  # 创建模型
        print("模型创建结束，开始求解...")
        solutionStatus = psc.Solve()  # 求解模型

        if solutionStatus == True:  # 如果模型求解成功，则输出运行图数据
            ObjVal = psc.ObjVal
            psc.Data.WriteSolution()  # 输出求解结果
            psc.Data.WriteMetaData(Data.OutputFileDirectory+"/MetaData.xml")

# Data = pscdata.PSCData()
# DoTrainByTrainIteration()
